package com.kb.design.controller;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kb.design.dto.CustomUserDTO;
import com.kb.design.common.ApiResponse;
import com.kb.design.common.ResponseObject;
import com.kb.design.entity.BaseEntity;
import com.kb.design.entity.CustomUser;
import com.kb.design.entity.DesignOperateLog;
import com.kb.design.service.CustomUserService;
import com.kb.design.util.CompareObjectUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * (CustomUser)表控制层
 *
 * @author makejava
 * @since 2023-08-22 10:56:58
 */
@RestController
@RequestMapping("customUser")
public class CustomUserController {
    /**
     * 服务对象
     */
    @Resource
    private CustomUserService customUserService;

    /**
     * 分页查询（旧方法）
     *
     * @param customUser 筛选条件
     * @return 查询结果
     */
    @PostMapping("list")
    public ApiResponse queryByPage(@RequestBody CustomUserDTO customUser) {
        ResponseObject responseObject = new ResponseObject();
        Page<CustomUser> customUserPage = this.customUserService.queryByPage(customUser);
            responseObject.setItems(customUserPage.getRecords());
            responseObject.setTotal(customUserPage.getTotal());
        return ApiResponse.success(responseObject);
    }

    /**
     * 根据参数查询用户
     *
     * @param
     * @return 查询结果
     */
    @GetMapping("queryUserByDeptId")
    public ApiResponse queryUserByDeptId(@RequestParam(required = false) Long deptId,
                                         @RequestParam(required = false) String role) {
        return ApiResponse.success(customUserService.queryUserByParams(deptId, role));
    }
    /**
     * 查询所有用户
     *
     * @param
     * @return 查询结果
     */
    @PostMapping("selectAll")
    public ApiResponse selectAll() {
        return ApiResponse.success(customUserService.selectAll());
    }

    /**
     * 新增数据
     *
     * @param customUser 实体
     * @return 新增结果
     */
    @PostMapping("/add")
    public ApiResponse add(@RequestBody CustomUser customUser) {
        this.customUserService.insert(customUser);
        return ApiResponse.success("新增成功");
    }

    /**
     * 新增数据
     *
     * @param customUser 编辑
     * @return 新增结果
     */
    @PostMapping("/editor")
    public ApiResponse edi(@RequestBody CustomUser customUser) {
        this.customUserService.editor(customUser);
        return ApiResponse.success("编辑成功");
    }

    /**
     * 编辑数据
     *
     * @param customUser 实体
     * @return 编辑结果
     */
    @PostMapping("/edit")
    public ApiResponse edit(@RequestBody CustomUser customUser) {
        ResponseObject responseObject = new ResponseObject();
        this.customUserService.update(customUser);
        return ApiResponse.success(responseObject);
    }

    /**
     * 批量删除数据
     *
     * @return 删除是否成功
     */
    @PostMapping("/del/{ids}")
    public ApiResponse deleteById(@PathVariable("ids") String ids) {
        String[] split = ids.split(",");
        for (String id : split) {
            this.customUserService.deleteById(Long.valueOf(id));
        }
        return ApiResponse.success(new ResponseObject());
    }

    /**
     * 删除数据
     *
     * @return 删除是否成功
     */
    @GetMapping("/delUser")
    public ApiResponse delUser(String id) {
        customUserService.deleteById(Long.valueOf(id));
        return ApiResponse.success("删除成功");
    }

    /**
     * 查询百搭数据
     *
     * @return
     */
    @GetMapping("/queryBaiUser")
    public ApiResponse queryBaiUser(String code) {
        return ApiResponse.success(customUserService.queryBaiUser(code));
    }


    public static void main(String[] args) throws InterruptedException {
        BaseEntity baseEntity = new BaseEntity();
        baseEntity.setRemark("111111");
        baseEntity.setCreateTime(LocalDateTime.now());
        baseEntity.setUpdateBy("44444");

        BaseEntity OldbaseEntity = new BaseEntity();
        OldbaseEntity.setRemark("111111");
        Thread.sleep(1000);
        OldbaseEntity.setCreateTime(LocalDateTime.now());
        OldbaseEntity.setCreateBy("4444");
        //再写一个新值个旧值比较 判断是新增还是修改或者删除
        try {
            List<Map<String, Object>> maps = CompareObjectUtils.compareTwoClass(OldbaseEntity, baseEntity);//旧新数据对比
            for (Map<String, Object> map : maps) {
                DesignOperateLog designOperateLog = BeanUtil.mapToBean(map, DesignOperateLog.class, false);
                System.out.println(designOperateLog);
            }
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }
    }
}